package com.zzsy.carosshopprojects.adapter;

import android.content.Context;
import android.text.SpannableString;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.king.base.adapter.ViewHolderRecyclerAdapter;
import com.king.base.adapter.holder.ViewHolder;
import com.yatoooon.screenadaptation.ScreenAdapterTools;
import com.zzsy.carosshopprojects.R;
import com.zzsy.carosshopprojects.bean.CouponUserdBean;
import com.zzsy.carosshopprojects.util.ConverterHelper;
import com.zzsy.carosshopprojects.util.StringUtils;

import java.math.BigDecimal;
import java.util.List;

public class CouponListAdapter extends ViewHolderRecyclerAdapter<CouponUserdBean> {
    private   int fullAmount = 0;
    public CouponListAdapter(Context context, List<CouponUserdBean> listData, int layoutId) {
        super(context, listData, layoutId);
    }

    @Override
    public View buildConvertView(LayoutInflater layoutInflater, ViewGroup parent, int viewType) {
        View view = layoutInflater.inflate(R.layout.adapter_couponlist, null);
        ScreenAdapterTools.getInstance().loadView(view);
        return view;
    }

    @Override
    public void bindViewDatas(ViewHolder helper, CouponUserdBean couponUserdBean, int positions) {
        TextView tvCouponMoney = helper.getView(R.id.tv_coupon_money);
        TextView tvCouponName = helper.getView(R.id.coupon_name);
        ImageView mCheckBox = helper.getView(R.id.check_box);
        LinearLayout rlCounps = helper.getView(R.id.rl_view_bg);
        TextView tvCounpDate = helper.getView(R.id.tv_coupon_date);
        TextView isDJ = helper.getView(R.id.tv_coupon_dj);
        TextView tvOilsType = helper.getView(R.id.tv_oils_type);
        TextView tvFullAmount = helper.getView(R.id.tv_full_amount);
        View view = helper.getView(R.id.view);
        boolean show = couponUserdBean.isShow();
        if (!show) {
            tvCouponMoney.setTextColor(getContext().getResources().getColor(R.color.col_9a9a9a));
            tvCounpDate.setTextColor(getContext().getResources().getColor(R.color.col_9a9a9a));
            tvCouponName.setTextColor(getContext().getResources().getColor(R.color.col_9a9a9a));
            rlCounps.setBackground(getContext().getResources().getDrawable(R.drawable.coupon_bg_gray));
            mCheckBox.setImageResource(R.mipmap.grak_check);
            view.setBackgroundResource(R.mipmap.grak_lines);
        } else {
            tvCouponMoney.setTextColor(getContext().getResources().getColor(R.color.title_bg));
            tvCounpDate.setTextColor(getContext().getResources().getColor(R.color.title_bg));
            tvCouponName.setTextColor(getContext().getResources().getColor(R.color.title_bg));
            rlCounps.setBackground(getContext().getResources().getDrawable(R.drawable.coupon_bg_w));
            view.setBackgroundResource(R.mipmap.oragen_line);
            if (couponUserdBean.isCheck()) {
                mCheckBox.setImageResource(R.mipmap.checkbox_selected);
            } else {
                mCheckBox.setImageResource(R.mipmap.checkbox_unselected);
            }
        }
        int fullAmount = couponUserdBean.getFullAmount();
        String preType = couponUserdBean.getPreType();

        /**
         * 设置满多少钱可用
         * */
        BigDecimal bigDecimal1 = ConverterHelper.toBigDecimal(fullAmount).divide(ConverterHelper.toBigDecimal(100)).setScale(2, BigDecimal.ROUND_HALF_UP);
        double fullAmountDou = bigDecimal1.doubleValue();
        tvFullAmount.setText("满" + fullAmountDou + "元可用");
        /**
         * 设置优惠券金额
         * */
        int preAmount = couponUserdBean.getPreAmount();
        BigDecimal bigDecimal = ConverterHelper.toBigDecimal(preAmount).divide(ConverterHelper.toBigDecimal(100)).setScale(2, BigDecimal.ROUND_HALF_UP);
        double preAmountStr = bigDecimal.doubleValue();
        String preAmountDou = preAmountStr + "￥";
        SpannableString pre = StringUtils.setFontSizeSp(getContext(), preAmountDou, preAmountDou.length() - 1, preAmountDou.length(), 24);
        tvCouponMoney.setText(pre);

        /**
         * 有效期
         * */
        tvCounpDate.setText("有效期至" + couponUserdBean.getEndTime());
        /**
         * 优惠券名称
         * */
        tvCouponName.setText(couponUserdBean.getCouponName());
        mCheckBox.setVisibility(View.VISIBLE);

        String superposition = couponUserdBean.getSuperposition();
        if ("0".equals(superposition)) {
            isDJ.setText("不可叠加使用");
        } else if ("1".equals(superposition)) {
            isDJ.setText("可叠加使用");
        }
        // 设置优惠名称
        if ("1".equals(preType)) {
            tvOilsType.setText("柴油券");
        } else if ("2".equals(preType)) {
            tvOilsType.setText("汽油券");
        }
    }

    public void choseCopun(int payAmount, int pos, List<CouponUserdBean> myLiveList) {
        if (!StringUtils.isEmpty(myLiveList.get(pos).getSuperposition())) {
            if ("0".equals(myLiveList.get(pos).getSuperposition())) {
                /**
                 * 更新选中的优惠券 添加选中未选中标识
                 * */
                CouponUserdBean myLive = myLiveList.get(pos);
                boolean isSelect = myLive.isCheck();
                if (!isSelect) {
                    myLive.setCheck(true);
                    myLive.setShow(true);
                } else {
                    myLive.setCheck(false);
                    myLive.setShow(false);
                }
                /**
                 *  不可叠加优惠券的处理
                 * 判断当前是否置灰的
                 * 如果是置灰的  相当于恢复默认值
                 * 如果不是置灰的 把其他的置灰
                 * */
                if (!myLiveList.get(pos).isShow()) {
                    for (int i = 0; i < myLiveList.size(); i++) {
                        myLiveList.get(i).setShow(true);
                    }
                } else {
                    for (int i = 0; i < myLiveList.size(); i++) {
                        if (i == pos && myLiveList.get(pos).isShow()) {
                            myLiveList.get(i).setShow(true);
                        } else {
                            myLiveList.get(i).setShow(false);
                        }
                    }
                }
                notifyDataSetChanged();


            } else if ("1".equals(myLiveList.get(pos).getSuperposition())) {

                /**
                 * 更新选中的优惠券 添加选中未选中标识
                 * */
                CouponUserdBean myLive = myLiveList.get(pos);
                boolean isSelect = myLive.isCheck();
                if (!isSelect) {
                    myLive.setCheck(true);
                } else {
                    myLive.setCheck(false);
                }

                /**
                 * 如果全部为未选中 我们全部设置为高亮
                 * */
                boolean isGL = false;
                for (int i = 0; i < myLiveList.size(); i++) {
                    if (myLiveList.get(i).isCheck()) {
                        isGL = true;
                    }
                }
                /**
                 * 判断如果全是未选中 恢复高亮
                 * 否则 将不可叠加优惠券置灰
                 * */
                if (!isGL) {
                    for (int i = 0; i < myLiveList.size(); i++) {
                        myLiveList.get(i).setShow(true);
                    }
                } else {
                    /**
                     * 遍历优惠券集合 将不可叠加优惠券置灰
                     * */
                    for (int i = 0; i < myLiveList.size(); i++) {
                        if ("0".equals(myLiveList.get(i).getSuperposition())) {
                            myLiveList.get(i).setShow(false);
                        }
                    }
                }

                /**
                 * 判断选中的金额是否大于订单金额
                 * 将选中的金额进行累加 然后判断与订单金额的大小
                 * */


                if (!isSelect) {
                    fullAmount += myLiveList.get(pos).getFullAmount();
                    myLive.setCheck(true);
                } else {
                    fullAmount -= myLiveList.get(pos).getFullAmount();
                    myLive.setCheck(false);
                }

                /**
                 * 我们用订单金额减去选中金额  得出剩余金额
                 * 用剩余金额进行筛选 判断是否有符合的优惠券
                 * 如果有 进行高亮显示 否则置灰
                 * */
                //剩余额度
                int syAmount = payAmount - fullAmount;
                Log.d("xxxx",payAmount+"/"+fullAmount);
                Log.d("*****",syAmount+"");
                for (int i = 0; i < myLiveList.size(); i++) {
                    if ("1".equals(myLiveList.get(i).getSuperposition()) && !myLiveList.get(i).isCheck()) {
                        if (syAmount < myLiveList.get(i).getFullAmount()) {
                            myLiveList.get(i).setShow(false);
                        } else {
                            myLiveList.get(i).setShow(true);
                        }
                    }
                }

                notifyDataSetChanged();
            }
        }
    }

}
